﻿using Autofac;
using Org.BouncyCastle.Asn1.Crmf;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
//using System.Windows;

//using System.Windows.Controls;
using System.Windows.Forms;
using WinformDevFramework.Core.Winform;
using WinformDevFramework.IServices;

namespace WinformDevFramework.Pages.Systemic
{
    public partial class FrmSelection : Form
    {
        private ISystemSelectServices _sysSelectServices;

        public FrmSelection(ISystemSelectServices sysSelectServices)
        {
            _sysSelectServices = sysSelectServices;
            InitializeComponent();
        }

        /// <summary>
        /// 重写初始化
        /// </summary>
        public void Init()
        {
            //调用基类中的Init
            //base.Init();

            //查询数据
            //this.dataGridViewList.DataSource= GetData();

            // 创建一个新的 FlowLayoutPanel
            FlowLayoutPanel flowLayoutPanel = new FlowLayoutPanel
            {
                Dock = DockStyle.Fill, // 让 FlowLayoutPanel 填充整个 TabPage
                FlowDirection = FlowDirection.LeftToRight, // 设置为从(上到下排列TopDown)（左到右 LeftToRight 或其他方向）
                AutoScroll = true, // 如果按钮太多以至于不能全部显示，则启用滚动条
                WrapContents = false // 如果按钮在一行内排满后再换行，则设置为 true；否则，每个按钮都会占据一行
            };
            Controls.Add(flowLayoutPanel);

            //SetToolButtonStatus(formStatus);
            List<sysSystemSelect> list = new List<sysSystemSelect>();
            list=_sysSelectServices.Query();
            foreach (var item in list)
            {
                Button btn = new Button
                {
                    Text = item.SystemName, // 使用Form的标题作为按钮文本
                    //Image = Image.FromFile(Application.StartupPath + "\\" + item.SystemImage), // 设置按钮的图像
                    Tag = item.ID, // 使用Form的ID作为按钮的标签，以便在点击时识别
                    FlatStyle = FlatStyle.Flat,
                    Font = new Font("Microsoft YaHei UI", 10.5F, FontStyle.Bold, GraphicsUnit.Point),
                    Location = new Point(0, 0),
                    Name = "button1",
                    Size = new Size(103, 61),
                    TabIndex = 1,
                    TextAlign = ContentAlignment.BottomCenter,
                    TextImageRelation = TextImageRelation.ImageAboveText,
                    UseVisualStyleBackColor = true,
                };
                // 为按钮添加点击事件处理程序,跳转到设置相应的 URL 页面
                btn.Click += (s, args) =>
                {

                    if ((item.SystemType!=null)&&item.SystemType=="1")
                    {
                        //打开网址
                        Process.Start(new ProcessStartInfo(item.SystemUrl) { UseShellExecute = true });
                    }
                    else
                    {
                        MessageBox.Show(item.SystemName);
                    }
                    
                };

                //// 将按钮添加到 Panel 中
                //panel.Controls.Add(btn);
                flowLayoutPanel.Controls.Add(btn);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string url = "http://10.0.0.104/";
                Process.Start(new ProcessStartInfo(url) { UseShellExecute = true });
            }
            catch (Exception ex)
            {
                MessageBox.Show("无法打开网页: " + ex.Message);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            // 指定要启动的应用程序的路径
            string applicationPath = @"C:\Users\xiaoyw\Desktop\DMSkin-master\DMSkin-master\Build\AduDesignDemo.exe";

            try
            {
                // 启动应用程序
                Process.Start(applicationPath);
            }
            catch (Exception ex)
            {
                // 处理启动应用程序时可能发生的异常
                MessageBox.Show($"无法启动应用程序: {ex.Message}");
            }
        }

        private void FrmSelection_Load(object sender, EventArgs e)
        {
            Init();
        }
    }
}
